//
//  MyLoadingView.m
//  Loading
//
//  Created by Hunk on 11-3-15.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MyLoadingView.h"
#import "MyTravelAppDelegate.h"
#import <QuartzCore/QuartzCore.h>

#define TAG_ACTIVITYINDICATORVIEW 1
#define TAG_LABEL 2
#define MESSAGE_LOADING @"信息加载中"

static MyLoadingView* _sharedMyLoadingView = nil;

@implementation MyLoadingView

+ (MyLoadingView*)sharedMyLoadingView
{
	@synchronized([MyLoadingView class])
	{
		if(!_sharedMyLoadingView)
		{
			_sharedMyLoadingView = [[MyLoadingView alloc] init];
		}
		return _sharedMyLoadingView;
	}
	return nil;
}

- (id)init
{
	if((self = [super init]))
	{
		self.frame = CGRectMake(0, 0, 320.0, 480.0);
		
		self.backgroundColor = [UIColor clearColor];
        
		// Add Bg
		UIView* loadingBGView = [[UIView alloc] initWithFrame:CGRectMake((self.frame.size.width - 100.0)/2, (self.frame.size.height - 100.0)/2, 100.0, 100.0)];
		loadingBGView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.8];
		loadingBGView.layer.cornerRadius = 10.0;
		loadingBGView.layer.masksToBounds = YES;
//        loadingBGView.layer.borderWidth = 2.0;
//        loadingBGView.layer.borderColor = [UIColor brownColor].CGColor;
		[self addSubview:loadingBGView];
		[loadingBGView release];
		
		// Create ActivityIndicator
		UIActivityIndicatorView* activityIndicatorView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake((self.frame.size.width - 30.0)/2, (self.frame.size.height - 30.0)/2 - 15, 30.0, 30.0)];
		activityIndicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
		activityIndicatorView.tag = TAG_ACTIVITYINDICATORVIEW;
		if(self)
		{
			[activityIndicatorView startAnimating];
			[self addSubview:activityIndicatorView];
		}
		[activityIndicatorView release];
		
		// Add name label
		UILabel* m_pNameLabel = [[UILabel alloc] initWithFrame:CGRectMake((self.frame.size.width - 100.0)/2, (self.frame.size.height - 60.0)/2 + 25, 100.0, 60.0)];
		m_pNameLabel.backgroundColor = [UIColor clearColor];
		m_pNameLabel.textColor = [UIColor whiteColor];
		m_pNameLabel.font = [UIFont systemFontOfSize:18];
		m_pNameLabel.textAlignment = UITextAlignmentCenter;
		m_pNameLabel.text = MESSAGE_LOADING;
		m_pNameLabel.tag = TAG_LABEL;
		m_pNameLabel.shadowColor = [UIColor blackColor];
		m_pNameLabel.shadowOffset = CGSizeMake(1.0, 1.0);
		[self addSubview:m_pNameLabel];
		[m_pNameLabel release];
		
		// Add to main window
		MyTravelAppDelegate* appDelegate = (MyTravelAppDelegate*)[[UIApplication sharedApplication] delegate];
		[appDelegate.window addSubview:self];
	}
	return self;
}

- (void)startLoadingAnimation
{	
	if(!self) return;
    
//    NSLog(@"%@\n", [self subviews]);
    
	[[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];

	self.hidden = NO;
	
	UIActivityIndicatorView* activityIndicatorView = (UIActivityIndicatorView*)[self viewWithTag:TAG_ACTIVITYINDICATORVIEW];
	
	[activityIndicatorView startAnimating];
	
	[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

- (void)startLoadingAnimationWithName:(NSString*)name backgroundImage:(NSString*)imageName
{
    if(!self) return;
    
    UIImageView* welcomeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    [welcomeImageView setBackgroundColor:[UIColor clearColor]];
    [welcomeImageView setImage:[UIImage imageNamed:imageName]];
    [self addSubview:welcomeImageView];
    [self sendSubviewToBack:welcomeImageView];
    [welcomeImageView release];
    
	[[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
    
	self.hidden = NO;
	
	UIActivityIndicatorView* activityIndicatorView = (UIActivityIndicatorView*)[self viewWithTag:TAG_ACTIVITYINDICATORVIEW];
	
    // NameLebel
    UILabel* nameLebel = (UILabel*)[self viewWithTag:TAG_LABEL];
    nameLebel.text = name;
    
	[activityIndicatorView startAnimating];
	
	[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

- (void)startLoadingAnimationWithName:(NSString*)name
{	
	if(!self) return;
	
	[[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
    
	self.hidden = NO;
	
    // UIActivityIndicatorView
	UIActivityIndicatorView* activityIndicatorView = (UIActivityIndicatorView*)[self viewWithTag:TAG_ACTIVITYINDICATORVIEW];
	
	[activityIndicatorView startAnimating];
    
    // NameLebel
    UILabel* nameLebel = (UILabel*)[self viewWithTag:TAG_LABEL];
    nameLebel.text = name;
	
	[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

- (void)stopLoadingAnimation
{
//    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    
    for(UIView* subview in [self subviews])
    {
        if([subview isKindOfClass:[UIImageView class]])
        {
            [subview removeFromSuperview];
        }
        else if([subview isKindOfClass:[UILabel class]])
        {
            UILabel* label = (UILabel*)subview;
            if(![label.text isEqualToString:MESSAGE_LOADING])
            {
                [label setText:MESSAGE_LOADING];
            }
        }
    }
    
	if(!self) return;
	
	self.hidden = YES;
	
	UIActivityIndicatorView* activityIndicatorView = (UIActivityIndicatorView*)[self viewWithTag:TAG_ACTIVITYINDICATORVIEW];
	
	[activityIndicatorView stopAnimating];
	
	[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
	
}

- (void)dealloc
{	
	[super dealloc];
}

@end
